<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <style>
        table {
            width: 1205px;
            margin: 30px auto;
            border-collapse: collapse;
            border: 1px solid #ccc;
            font-size: 18px;
        }

        th {
            background: #dad9d9;
            height: 6.0em;
            margin: 0 auto;
            border: 1px solid #000;
        }

        tr {
            height: 5.0em;
            margin: 0 auto;
            text-align: center;
        }

        td {
            border: 1px solid #000;
        }

        #shanchu {
            /* background-color: #ff4554; */
            color: red;
            cursor: pointer;
        }

        #goodsImg img {
            width: 200px;
            height: 200px;
        }

        #bnt {
            display: inline-block;
            width: 17px;
            color: #fff;
            background-color: #ccc;

        }

        #jiesuan {
            background-color: #ff4554;
            font-size: 18px;
            color: #fff;
            font-weight: 700;
        }
    </style>

</head>

<body>
    <!-- 头部模块  start -->
    <header class="header"></header>
    <!-- 头部模块  end -->
    <form>
        <table id="box">
            <tr>

                <th>全选</th>
                <th>商品序号</th>
                <th>商品名称</th>
                <th>商品图片</th>
                <th>商品数量</th>
                <th>商品单价</th>
                <th>商品小计</th>
                <th>操作</th>

            </tr>
            <!-- <tr>
                htmlStr 
            </tr>
            <tr>
                
            </tr> -->
        </table>
    </form>

    <!-- 尾部模块 start -->
    <footer class="footer"></footer>
    <!-- 尾部模块 end -->

</body>

</html>
<script src="jQuery.js"></script>
<script>
    // 头部模块
    $('header').load('header.html')

    // 尾部模块
    $('footer').load('footer.html')




    $(function () {
        //拿数据
        getShoppingCar();
    })
    //购物车
    function getShoppingCar() {
        $.get(
            "goodsAndShoppingCart/getShoppingCart.php",
            {
                vipName: "banggou"
            },
            function (data) {
                let htmlStr = "";
                let Money = 0;
                console.log(data)
                data.forEach(goods => {

                    //小计
                    let XiaoJi = goods.goodsPrice * goods.goodsCount;
                    //合计
                    Money += XiaoJi;
                    htmlStr += ` 
                                 <tr>
                                    <td></td>
                                    <td>${goods.goodsId}</td>
                                    <td>${goods.goodsName}</td>
                                    <td id="goodsImg"><img src="${goods.goodsImg}" alt=""></td>
                                    <td>
                                        <button id='bnt'; onclick="reduceCount(this,'${goods.goodsId}')">-</button>
                                        <span class="goods-num">${goods.goodsCount}</span>
                                        <button id="bnt" onclick="add(this,'${goods.goodsId}')">+</button>
                                    </td>
                                    <td>
                                        <span>￥:</span>
                                        <span>${goods.goodsPrice}</span>
                                    </td>
                                    <td >
                                        <span class="xiaoji">￥${XiaoJi}</span>
                                    </td>
                                    <td id="shanchu" onclick="shanchu(this,'${goods.goodsId}')">删除</td>
                                 </tr>
                            `;
                });
                console.log($(".xiaoji"));

                htmlStr += `
                                 <tr>
                                    <td colspan="5">
                                        <span>总价：<span class="money">${Money}</span>元</span>
                                    </td>
                                    <td id="jiesuan" colspan="3">结算</td>
                                    <td></td>
                                 </tr>
                            `;


                $("#box tbody").html($("#box tbody").html() + htmlStr);
            }, "json")
    }


    //发请求做加减法
    // 点击减号
    function reduceCount(btn, goodsId) {
        let count = parseInt(btn.nextElementSibling.innerHTML)
        count--;
        if (count <= 1) {
            count = 1
        }
        //修改后端数量
        $.get(
            "goodsAndShoppingCart/updateGoodsCount.php",
            {
                vipName: "banggou",
                goodsId: goodsId,
                goodsCount: count
            },
            function (data) {
                if (data == 1) {
                    //修改后端数量
                    //数量赋给dom
                    btn.nextElementSibling.innerHTML = count;

                    //变小计
                    //单价
                    let price = parseInt(btn.parentNode.nextElementSibling.lastElementChild.innerHTML.substring
                        (1));
                    console.log("price", price);
                    let xiao = price * count;
                    btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild.firstElementChild.innerHTML = xiao;
                    // console.log("xiao", xiao);
                    //总价
                    tomaney();
                }
            }
        )
    }
    //合计
    function tomaney() {

        let moneys = 0;
        let oxiaoji = $(".xiaoji");
        for (let i = 0; i < oxiaoji.length; i++) {
            console.log("oxiaoji[i].innerHTML", oxiaoji[i].innerHTML)

            moneys += parseInt(oxiaoji[i].innerHTML)
        }
        console.log("oxiaoji.length", oxiaoji.length)
        // console.log(moneys)
        $(".money").html(moneys);
    }


    //加法
    function add(btn, goodsId) {
        let count = parseInt(btn.previousElementSibling.innerHTML)
        count++;

        //修改后端数量
        $.get("goodsAndShoppingCart/updateGoodsCount.php",
            {
                vipName: "banggou",
                goodsId: goodsId,
                goodsCount: count
            },
            function (data) {
                if (data == 1) {
                    //修改后端数量
                    //数量赋给dom
                    btn.previousElementSibling.innerHTML = count;
                    //变小计
                    //单价
                    let price = parseInt(btn.parentNode.nextElementSibling.lastElementChild.innerHTML);
                    // console.log("price", price);
                    let xiao = price * count;
                    btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild.firstElementChild.innerHTML = xiao;
                    console.log("xiao", xiao);
                    //总价
                    tomaney();
                }
            }
        )
    }


    //按下删除键，就删除此时的商品


    function shanchu(btn, goodsId) {
        $.get("goodsAndShoppingCart/deleteGoods.php", {
            vipName: "banggou",
            goodsId: goodsId,
        }, function (data) {
            if (data == 1) {
                btn.parentNode.parentNode.remove(this)
                // totalPriceAndNum();
            }
        })
    }

</script>